
const {ccclass, property} = cc._decorator;

import mainCtrl from "./main";

@ccclass
export default class NewClass extends cc.Component {

    @property
    readonly BULLET_WIDTH: number = 16;

    @property
    readonly BULLET_HEIGHT: number = 30;

    @property
    speed: number = 10;

    @property
    main: mainCtrl = null;


    // Helpers
    moveBy(duration: number, pos: cc.Vec2){
        this.node.runAction(cc.moveBy(duration, pos));
    }

    isOutbounded(): boolean{
        // Boundary Check
        let posY = this.node.getPositionY();
        let screenSize = cc.director.getWinSize();
        if(posY >= screenSize.height/2){
            return true;
        }

        // Collision Check

        return false;
    }

    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        this.node.setContentSize(cc.size(this.BULLET_WIDTH, this.BULLET_HEIGHT));
    }

    start () {

    }
    onCollisionEnter (){
        this.node.destroy();
        this.destroy();
    }

    update (dt) {
        if(this.lastFiredTimer >= this.fireDuration){
            this.main.fire();
            this.lastFiredTimer = 0;
        }else{
            this.lastFiredTimer += dt;
        }

        this.moveBy(dt, cc.p(0, this.speed));
        if(this.isOutbounded()){
            this.node.destroy();
            this.destroy();
            return;
        }
    }
}
